package org.wu.framework.authorization.platform.application.assembler;

import org.wu.framework.authorization.platform.application.command.SysMenuCommand;
import org.wu.framework.authorization.platform.application.dto.SysMenuDTO;
import org.wu.framework.authorization.platform.application.dto.SysMenuTreeDTO;
import org.wu.framework.authorization.platform.domain.model.sys.menu.SysMenu;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyAssembler;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * describe
 *
 * @author Jia wei Wu
 * @date 2023/05/17 11:19 晚上
 * @see DefaultDDDLazyAssembler
 **/

public class SysMenuDTOAssembler {


    /**
     * describe 应用层入参转换成 持久层入参
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/
    public static SysMenu toMenu(SysMenuCommand sysMenuCommand) {
        if (null != sysMenuCommand) {
            SysMenu sysMenu = new SysMenu();
            sysMenu.setDesc(sysMenuCommand.getDesc());
            sysMenu.setCode(sysMenuCommand.getCode());
            sysMenu.setCreateTime(sysMenuCommand.getCreateTime());
            sysMenu.setIcon(sysMenuCommand.getIcon());
            sysMenu.setId(sysMenuCommand.getId());
            sysMenu.setIframe(sysMenuCommand.getIframe());
            sysMenu.setMenu(sysMenuCommand.getMenu());
            sysMenu.setName(sysMenuCommand.getName());
            sysMenu.setParentCode(sysMenuCommand.getParentCode());
            sysMenu.setSort(sysMenuCommand.getSort());
            sysMenu.setType(sysMenuCommand.getType());
            sysMenu.setUpdateTime(sysMenuCommand.getUpdateTime());
            sysMenu.setUrl(sysMenuCommand.getUrl());
            sysMenu.setModule(sysMenuCommand.getModule());
            return sysMenu;
        }
        return null;
    }

    /**
     * describe 持久层领域对象转换成DTO对象
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/
    public static SysMenuDTO fromMenu(SysMenu sysMenu) {
        if (null != sysMenu) {
            SysMenuDTO sysMenuDTO = new SysMenuDTO();
            sysMenuDTO.setDesc(sysMenu.getDesc());
            sysMenuDTO.setCode(sysMenu.getCode());
            sysMenuDTO.setCreateTime(sysMenu.getCreateTime());
            sysMenuDTO.setIcon(sysMenu.getIcon());
            sysMenuDTO.setId(sysMenu.getId());
            sysMenuDTO.setIframe(sysMenu.getIframe());
            sysMenuDTO.setIsDeleted(sysMenu.getIsDeleted());
            sysMenuDTO.setMenu(sysMenu.getMenu());
            sysMenuDTO.setName(sysMenu.getName());
            sysMenuDTO.setParentCode(sysMenu.getParentCode());
            sysMenuDTO.setSort(sysMenu.getSort());
            sysMenuDTO.setType(sysMenu.getType());
            sysMenuDTO.setUpdateTime(sysMenu.getUpdateTime());
            sysMenuDTO.setUrl(sysMenu.getUrl());
            sysMenuDTO.setModule(sysMenu.getModule());
            return sysMenuDTO;
        }
        return null;
    }
    public static SysMenuTreeDTO treeFromMenu(SysMenu sysMenu) {
        if (null != sysMenu) {
            SysMenuTreeDTO sysMenuTreeDTO = new SysMenuTreeDTO();
            sysMenuTreeDTO.setDesc(sysMenu.getDesc());
            sysMenuTreeDTO.setCode(sysMenu.getCode());
            sysMenuTreeDTO.setCreateTime(sysMenu.getCreateTime());
            sysMenuTreeDTO.setIcon(sysMenu.getIcon());
            sysMenuTreeDTO.setId(sysMenu.getId());
            sysMenuTreeDTO.setIframe(sysMenu.getIframe());
            sysMenuTreeDTO.setIsDeleted(sysMenu.getIsDeleted());
            sysMenuTreeDTO.setMenu(sysMenu.getMenu());
            sysMenuTreeDTO.setName(sysMenu.getName());
            sysMenuTreeDTO.setParentCode(sysMenu.getParentCode());
            sysMenuTreeDTO.setSort(sysMenu.getSort());
            sysMenuTreeDTO.setType(sysMenu.getType());
            sysMenuTreeDTO.setUpdateTime(sysMenu.getUpdateTime());
            sysMenuTreeDTO.setUrl(sysMenu.getUrl());
            sysMenuTreeDTO.setModule(sysMenu.getModule());
            return sysMenuTreeDTO;
        }
        return null;
    }

    public static List<SysMenuTreeDTO> getChildren(SysMenuTreeDTO sysMenuTreeDTO, List<SysMenuTreeDTO> sysMenuTreeDTOList){
        return sysMenuTreeDTOList.stream().filter(u -> u.getType() != 2).filter(u -> Objects.equals(u.getParentCode(), sysMenuTreeDTO.getCode())).peek(
                u -> {
                    List<SysMenuTreeDTO> buttonList = sysMenuTreeDTOList.stream()
                            .filter(menu -> Objects.equals(menu.getParentCode(), u.getCode()))
                            .filter(menu -> menu.getType() == 2)
                            .collect(Collectors.toList());
                    u.setChildren(getChildren(u, sysMenuTreeDTOList));
                    u.setButtonList(buttonList);

                }
        ).collect(Collectors.toList());
    }

    public static List<SysMenuTreeDTO> treeFromMenu(List<SysMenu> sysMenuList) {
        List<SysMenuTreeDTO> sysMenuTreeDTOList = sysMenuList.stream().parallel().map(SysMenuDTOAssembler::treeFromMenu).collect(Collectors.toList());
        SysMenuTreeDTO sysMenuTreeDTO = new SysMenuTreeDTO();
        sysMenuTreeDTO.setCode("-1");
        return getChildren(sysMenuTreeDTO, sysMenuTreeDTOList);
    }
}